package top.pengdong.pictureShare.userAuth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.pengdong.pictureShare.common.model.PageUtils;
import top.pengdong.pictureShare.common.model.Query;
import top.pengdong.pictureShare.common.model.R;
import top.pengdong.pictureShare.userAuth.dao.TUserRelationDao;
import top.pengdong.pictureShare.userAuth.dao.TUsersDao;
import top.pengdong.pictureShare.userAuth.entity.TUserRelationEntity;
import top.pengdong.pictureShare.userAuth.entity.TUsersEntity;
import top.pengdong.pictureShare.userAuth.service.TUserRelationService;
import top.pengdong.pictureShare.userAuth.service.TUsersService;

import java.util.Map;


@Service("tUserRelationService")
public class TUserRelationServiceImpl extends ServiceImpl<TUserRelationDao, TUserRelationEntity> implements TUserRelationService {


    @Autowired
    private TUsersDao tUsersDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TUserRelationEntity> page = this.page(
                new Query<TUserRelationEntity>().getPage(params),
                new QueryWrapper<TUserRelationEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 获取用户 关注列表
     * @param userId
     * @param currPage
     * @param limit
     * @return
     */
    @Override
    public R getUserRelationInfo(Long userId, Integer currPage, Integer limit, String type) {
        IPage<TUserRelationEntity> page = this.page(
                new Page<TUserRelationEntity>(currPage, limit),
                new QueryWrapper<TUserRelationEntity>().eq(type, userId)
        );
        // TUsersEntity tUsers = tUsersDao.selectById(userId);
        for (TUserRelationEntity tUserRelation : page.getRecords()) {
            getUserInfoUrlUsername(tUserRelation);
        }
        return R.ok("返回用户关注列表").put("result", new PageUtils(page));
    }


    private void getUserInfoUrlUsername(TUserRelationEntity tUserRelation) {
        TUsersEntity tUsers = tUsersDao.selectById(tUserRelation.getFollowId());
        tUserRelation.setToNickname(tUsers.getNickname());
        tUserRelation.setDescription(tUsers.getDescription());
        tUserRelation.setToUserAvatar(tUsers.getAvatar() == null ? "" : tUsers.getAvatar());
    }
}